<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small"
                      ref="multipleTable"
                      current-row-key="id"
                      :data="dataList"
                      row-key="id"
                      border
                      lazy
                      :load="load"
                      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                      stripe highlight-current-row>
                <!--                <el-table-column type="index" align="center" label="序号"></el-table-column>-->
                <el-table-column prop="title" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>
                <el-table-column prop="path" label="路径级别" align="center"></el-table-column>
                <el-table-column prop="parentMenuName" label="自关联名称" width="60" align="center"></el-table-column>
                <el-table-column prop="level" label="菜单级别" width="60px" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        <!--                        <el-button size="mini" type="warning" @click="handleRole(scope.row)">添加角色</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="title">
                                    <el-input v-model="formData.title"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input id="pathp"  v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item  label="优先级" prop="priority">
                                    <el-input id="priorityp" v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="自关联名称" prop="parentMenuName">
                                    <el-select v-model="formData.parentMenuId" @change="f()" placeholder="请选择自关联ID">
                                        <el-option label="无" value="0"></el-option>
                                        <el-option
                                                v-for="item in parentMenuIdData"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id"
                                                :disabled=false
                                        >
                                        </el-option>
                                    </el-select>
                                </el-form-item>

                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单级别" prop="level">
                                    <el-select v-model="formData.level"  placeholder="请选择菜单级别">
                                        <el-option label="一级菜单" value="1"></el-option>
                                        <el-option label="二级菜单" value="2"></el-option>
                                        <!--                                        <el-option label="三级菜单" value="beijing"></el-option>-->
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item label="图标">
                                    <el-input id="iconq" v-model="formData.icon"  ></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-dropdown trigger="click" v-model="formData.icon"  @command="handleCommand">
                                    <el-button >
                                        选择图标<i class="el-icon-arrow-down el-icon--right"></i>
                                    </el-button>
                                    <el-dropdown-menu  slot="dropdown">
                                        <el-dropdown-item v-for="item in iconData"
                                                          :command="item.name" :icon="item.id"
                                        >{{item.name}}</el-dropdown-item>
                                    </el-dropdown-menu>
                                </el-dropdown>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="注意事项">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="title">
                                    <el-input v-model="formData.title"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="自关联Id" prop="parentMenuId">
                                    <el-select v-model="formData.parentMenuId" placeholder="请选择自关联ID">
                                        <el-option
                                                v-for="item in parentMenuIdData"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id"
                                                :disabled=false
                                        >
                                        </el-option>
                                    </el-select>
                                </el-form-item>

                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单级别" prop="level">
                                    <el-select v-model="formData.level" placeholder="请选择菜单级别">
                                        <el-option label="一级菜单" value="1"></el-option>
                                        <el-option label="二级菜单" value="2"></el-option>
                                        <!--                                        <el-option label="三级菜单" value="beijing"></el-option>-->
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标">
                                    <el-input v-model="formData.icon" ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="注意事项">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--            修改绑定权限-->
            <div class="add-form">
                <el-dialog title="修改角色绑定" :visible.sync="dialogFormVisibleRole">
                    <template>
                        <!--                        <el-tab-pane label="角色信息" name="second">-->
                        <div class="checkScrol">
                            <table class="datatable">
                                <thead>
                                <tr>
                                    <th>选择</th>
                                    <th>角色名称</th>
                                    <th>关键词</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="c in tableData">
                                    <td>
                                        <input :id="c.id" v-model="checkMenuIds" type="checkbox" :value="c.id">
                                    </td>
                                    <td><label :for="c.id">{{c.name}}</label></td>
                                    <br/>&nbsp;  &nbsp;&nbsp;&nbsp;
                                    <td><label :for="c.id">{{c.keyword}}</label></td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        <!--                        </el-tab-pane>-->
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleRole = false">取消</el-button>
                        <el-button type="primary" @click="handleAddRole()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/element-ui/lib/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<script>
    var vue = new Vue({
            el: '#app',
            data: {
                pagination: {//分页相关模型数据
                    currentPage: 1,//当前页码
                    pageSize: 10,//每页显示的记录数
                    total: 0,//总记录数
                    queryString: null//查询条件
                },
                parentMenuIdData:[
                    {id:1,name:"aa"},
                    {id:2,name:"bb"},
                    {id:3,name:"cc"}
                ],
                tableData:{},//所有角色
                checkMenuIds:[],//该菜单选择的角色
                menuidHidden:'',
                iconData:[
                    {id:"fa fa-ambulance",name:"fa-ambulance"},
                    {id:"fa fa-h-square",name:"fa-h-square"},
                    {id:"fa fa-heart",name:"fa-heart"},
                    {id:"fa fa-heart-o",name:"fa-heart-o"},
                    {id:"fa fa-heartbeat",name:"fa-heartbeat"},
                    {id:"fa fa-hospital-o",name:"fa-hospital-o"},
                    {id:"fa fa-medkit",name:"fa-medkit"},
                    {id:"fa fa-plus-square",name:"fa-plus-square"},
                    {id:"fa fa-stethoscope",name:"fa-stethoscope"},
                    {id:"fa fa-user-md",name:"fa-user-md"},
                    {id:"fa fa-wheelchair",name:"fa-wheelchair"},
                    {id:"fa fa-wheelchair-alt",name:"fa-wheelchair-alt"}

                ],//
                dataList: [],//当前页要展示的分页列表数据
                formData: {
                    icon:{}
                },//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit: false,//编辑表单是否可见
                dialogFormVisibleRole:false,//绑定角色界面
                rules: {//校验规则
                    name: [{required: true, message: '名称为必填项', trigger: 'blur'}],
                    linkUrl: [{required: true, message: '路径为必填项', trigger: 'blur'}],
                    path: [{required: true, message: '必填项', trigger: 'blur'}],
                    priority: [{required: true, message: '必填项', trigger: 'blur'}],
                    parentMenuId: [{required: true, message: '必填项', trigger: 'blur'}],
                    level: [{required: true, message: '必填项', trigger: 'blur'}],
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                this.findPage();
            },
            methods: {
                f(){
                    if(this.formData.parentMenuId=='0')
                    {
                        this.formData.level="1";
                    }else{
                        this.formData.level="2";
                    }

                    //根据parentMenuId获取path 和 priority
                    axios.get("/menu/getInfo.do?parentMenuId="+this.formData.parentMenuId).then((resq)=>{

                        this.formData.path=resq.data.path;
                        this.formData.priority=resq.data.priority;
                        $('#pathp').val(resq.data.path);
                        $('#priorityp').val(resq.data.priority);
                    });
                },

                handleCommand(command) {
                    this.formData.icon=command;
                    $('#iconq').val(command);

                },
                //权限不足提示
                showMessage(r){
                    if(r == 'Error: Request failed with status code 403'){
                        //权限不足
                        this.$message.error('无访问权限');
                        return;
                    }else{
                        this.$message.error('未知错误');
                        return;
                    }
                },
                //编辑
                handleEdit() {
                    if(this.formData.parentMenuId=="0"){
                        this.formData.parentMenuId=null;
                    }
                    this.$refs['dataEditForm'].validate(//数据校验
                        (vali)=>{
                            if(vali){
                                //校验通过，开始请求

                                axios.post("/menu/update.do",this.formData).then(
                                    (resq)=>{
                                        this.dialogFormVisible4Edit = false;
                                        if(resq.data.flag){
                                            //修改成功
                                            this.$message({type:"success",message:resq.data.message});
                                        }else {
                                            //修改失败
                                            this.$message({type:"warning",message:resq.data.message});
                                        }
                                    }
                                ).catch((r)=>{
                                    this.showMessage(r);
                                }).finally(()=>{
                                    //重新分页查询
                                    this.findPage();
                                });
                            }
                            else{
                                this.$message({
                                    type:"warning" ,
                                    message:'数据校验未通过'
                                });
                            }
                        }
                    );

                },
                //添加
                handleAdd() {
                    this.$refs['dataAddForm'].validate(
                        (valida) => {//表单校验
                            if (valida) {//校验成功
                                //ajax传递数据
                                if(this.formData.parentMenuId=="0"){
                                    this.formData.parentMenuId=null;
                                }
                                axios.post("/menu/add.do", this.formData).then(
                                    (resp) => {
                                        //无论新增成功失败，先把新增窗口隐藏
                                        this.dialogFormVisible = false;
                                        if (resp.data.flag) {
                                            //成功弹成功提示
                                            this.$message({
                                                message: resp.data.message,
                                                type: 'success'
                                            });
                                        } else {//新增失败弹失败警告
                                            this.$message({
                                                message: resp.data.message,
                                                type: 'warning'
                                            })
                                        }
                                    }
                                ).catch((r)=>{
                                    this.showMessage(r);
                                }).finally(
                                    () => {
                                        //无论失败还是成功，重新做分页查询
                                        this.findPage();
                                    }
                                );
                            } else {//校验失败
                                this.$message.error("表单校验失败！")
                                return false;
                            }
                        }
                    )
                },
                // 分页查询
                findPage() {
                    //分页参数
                    param = {
                        currentPage: this.pagination.currentPage, //页码
                        pageSize: this.pagination.pageSize,//每页记录数
                        queryString: this.pagination.queryString//查询条件
                    };
                    //请求后台
                    axios.post("/menu/findPage.do?level=1&parentMenuId=", param).then((resp) => {
                        // 遍历结果添加hasChildren属性，hasChildren为true 支持懒加载
                        list=resp.data.rows;
                        for(i=0;i<list.length;i++){
                            if(list[i].children.length>0){
                                list[i]["hasChildren"]=true;
                            }
                            list[i]["level"]=1;
                        }
                        this.dataList = list;//查询到的检查项数据
                        this.pagination.total = resp.data.total;//检查项总条数
                        //this.$refs.multipleTable.toggleRowExpansion(this.dataList,true);
                    }).catch((r)=>{
                        this.showMessage(r);
                    });
                },
                // 重置表单
                resetForm() {
                    this.formData = {};
                },
                // 弹出添加窗口
                handleCreate() {
                    this.resetForm();
                    this.dialogFormVisible = true;
                    axios.get("/menu/getParentMenuIdData.do").then(s=>{
                        this.parentMenuIdData=s.data;
                    }).catch(()=>{
                        this.$message.error("加载新增弹窗数据失败！")
                    });
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    //数据回填
                    axios.get("/menu/findbyid.do?id=" + row.id).then(
                        (resq) => {
                            if (resq.data.flag) {
                                this.formData = resq.data.data;
                                this.dialogFormVisible4Edit = true;
                            } else {//数据回填失败
                                this.$message.error(resq.data.message);
                            }
                        }
                    ).catch((r)=>{
                        this.showMessage(r);
                    });

                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                },
                // 删除
                handleDelete(row) {
                    this.$confirm("您确定要删除这条信息吗？", '提示', {type: "warning"}).then(
                        () => {
                            axios.get("/menu/delete.do?id=" + row.id).then(
                                (resq) => {
                                    if (resq.data.flag) {//删除成功
                                        this.$message({
                                            type: "success", message: resq.data.message
                                        });
                                    } else {//删除失败

                                        this.$message.error(resq.data.message);
                                    }
                                }
                            ).catch((r)=>{
                                this.showMessage(r);
                            }).finally(
                                () => {
                                    //无论失败还是成功，重新做分页查询
                                    this.findPage();
                                }
                            );
                            this.pagination.currentPage = 1;
                            this.findPage();
                        }
                    ).catch(() => {
                        this.findPage();
                        this.$message({
                            type: "success", message: "已取消删除"
                        });

                    });
                },
                // //弹出修改角色
                // handleRole(row) {
                //     if(row.level!="1"){
                //         this.$message({
                //             type: "warning", message: "建议修改顶级菜单角色"
                //         });
                //         return false;
                //     }
                //     this.dialogFormVisibleRole=true;
                //     axios.get("../role/selectRole.do").then((data) => {
                //         if (data.data.flag) {
                //             this.tableData=data.data.data;
                //         }else {
                //             this.$message.error(data.data.message);
                //         }
                //
                //     });
                //     axios.get("/menu/selectRole.do?menuid="+row.id).then((data)=>{
                //         this.checkMenuIds=data.data;
                //         this.menuidHidden=row.id;
                //     });
                // },
                // handleAddRole(){
                //     this.dialogFormVisibleRole=false;
                //     axios.get("/menu/addRoleforMenu.do?menuid="+this.menuidHidden+"&checkMenuIds="+this.checkMenuIds).then((data)=>{
                //         this.$message({type:"success",message:data.data.message});
                //     });
                // },
                //懒加载
                load(tree, treeNode, resolve) {
                    var pathString=tree.path;
                    var split = pathString.split("/");
                    var level=1;
                    if(split.length>0){
                        level+=split.length;
                    }else{
                        resolve([]);
                        return false;
                    }
                    param = {
                        currentPage: 1, //页码
                        pageSize: 20,//每页记录数
                        queryString: null//查询条件
                    };
                    //请求后台

                    axios.post("/menu/findPage.do?level="+level+"&parentMenuId="+tree.id, param).then((resp) => {
                        list=resp.data.rows;
                        for(i=0;i<list.length;i++){
                            if(list[i].children.length>0){
                                list[i]["hasChildren"]=true;
                            }else{
                                list[i]["hasChildren"]=false;
                            }
                            list[i]["level"]=level;
                        }
                        resolve(resp.data.rows);//查询到的检查项数据
                    }).catch((r)=>{
                        this.showMessage(r);
                    });
                }
            }
        }
    )
</script>
</html>